﻿using System;
using System.Configuration;
using System.Data;
using CoreSoft.Frame.Entity.http;
using FERROU8API.Pub;
using U8Login;
using UFIDA.U8.MomServiceCommon;

namespace FERROU8API.Plugin
{
    /// <summary>
    /// 委外订单子件
    /// </summary>
    public class OmmaterielsPlugin
    {
        private readonly string _ip = ConfigurationManager.AppSettings["WmsIP"] + "Moandommaterielsapi";
        public static string constr = "";
        private string url = "";

        private String GetCustJs(clsLogin login, MSXML2.IXMLDOMDocument2 domhead, int operation)
        {
            var datatable = Common.GetUrl(constr, login.get_cAcc_Id()).Tables[0];
            var datarow = datatable.Rows[0];
            url = datarow[1].ToString();
            var datatabledept = Common.GetUser(constr).Tables[0];
            DebugWriteLog.WriteLine("cAcc_Id:" + login.get_cAcc_Id() + ",count:" + datatabledept.Rows.Count);
            bool hasdept = false;
            foreach (DataRow item in datatabledept.Rows)
            {
                //部门code
                var deptcode = item[1].ToString();
                //过滤如果不是QZ不触发插件
                if (deptcode == login.get_cAcc_Id())
                {
                    DebugWriteLog.WriteLine("cAcc_Id:" + login.get_cAcc_Id() + ",deptcode:" + deptcode);
                    hasdept = true;
                }
            }
            if (!hasdept)//限制
            {
                return "Nopermission";
            }
            //MSXML2.IXMLDOMNode tmpNode = domhead.selectSingleNode("/customer");
            //String custcode = tmpNode.attributes.getNamedItem("ccuscode").nodeValue.ObjToString();
            //String custname = tmpNode.attributes.getNamedItem("ccusname").nodeValue.ObjToString();
            //string ccode = tmpNode.attributes.getNamedItem("ccccode").nodeValue.ObjToString();
            MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");

            int moid = tmphead.attributes.getNamedItem("moid").nodeValue.ObjToInt32();//委外订单ID
            int modetailsid = tmphead.attributes.getNamedItem("modetailsid").nodeValue.ObjToInt32();//委外订单子表ID 
            int momaterialsid = tmphead.attributes.getNamedItem("momaterialsid").nodeValue.ObjToInt32();//委外用料单子表ID

            string iquantity = tmphead.attributes.getNamedItem("iquantity").nodeValue.ObjToString();//应领数量
            string userCode = tmphead.attributes.getNamedItem("cpersonCode").nodeValue.ObjToString();//人员编码
            string bz = tmphead.attributes.getNamedItem("cexch_name").nodeValue.ObjToString();//币种 cexch_name 
            float exchangeRate = tmphead.attributes.getNamedItem("iexchrate").nodeValue.ObjToFloat();//汇率
            float taxRate = tmphead.attributes.getNamedItem("itaxrate").nodeValue.ObjToFloat();//税率 iTaxRate
            //string xxx = dombody

            //MSXML2.IXMLDOMNodeList tmpbody = dombody.selectNodes("//z:row");

            //List<Moandommaterielsapi> dlist = new List<Moandommaterielsapi>();

            //foreach (MSXML2.IXMLDOMNode item in tmpbody)
            //{
            //    int disSeq = item.attributes.getNamedItem("irowno").nodeValue.ObjToInt32();//行号
            //    string sourceDid = item.attributes.getNamedItem("autoid").nodeValue.ObjToString();//来源明细id
            //    string soCode = item.attributes.getNamedItem("autoid").nodeValue.ObjToString();//销售订单号
            //    Int32 StartFlag = 1;
            //    Moandommaterielsapi m = new Moandommaterielsapi();
            //    m.OmFlag = 1;
            //    dlist.Add(m);
            //}

            ApiRequest req = new ApiRequest();
            req.data = "";
            req.orgcode = login.get_cAcc_Id();
            req.primarykey = "";
            req.operation = operation;
            return req.ToJson();
        }
        public bool Update_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 2);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(_ip, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Insert_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 0);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(_ip, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Delete_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 1);
                //不是qz直接返回true
                if (js == "Nopermission")
                {
                    return true;
                }
                var result = HttpHelper.PostDataToServer(_ip, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
    }
}